package com.example.myapplication;

import com.example.myapplication.shortvideo.utils.GsonUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: rongfeng
 * @date: 2021/4/2
 * @description
 */
public class xgl {

    public static void main(String[] args) {
        File file = new File("C:\\Users\\demo\\Desktop\\json.txt");
        try {
            String str;
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            StringBuffer sb = new StringBuffer();
            String text = null;
            while ((text = bufferedReader.readLine()) != null) {
                sb.append(text);
            }
            str = sb.toString();

            JsonRootBean bean = GsonUtil.parse(str, JsonRootBean.class);
            for ( Data item:bean.getData()
                 ) {
                System.out.print(item.id+",");
            }
            System.out.println(bean.getData().size());
//            new Down().start(bean.getData().subList(0,1));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




    static class Down {
        volatile int mIndex = -1;
        ExecutorService pool;
        List<Data> urlList;

        public void start(List<Data> data) {
            urlList = data;
            pool = Executors.newFixedThreadPool(3);
            for (int i = 0; i < 1; i++) {
                next();
            }
        }

        public void next() {
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    mIndex++;
                    if (mIndex < urlList.size()) {
                        download(urlList.get(mIndex));
                    }
                }
            });
        }

        public void download(Data data) {
            // 下载网络文件
            int bytesum = 0;
            int byteread = 0;

            URL url = null;
            try {
                url = new URL(data.getAm_audio());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            InputStream inStream = null;
            FileOutputStream fs = null;
            try {
                URLConnection conn = url.openConnection();
                inStream = conn.getInputStream();


                String name = data.getBook_id() + "_" + data.getNo();
                fs = new FileOutputStream("D:\\wrf\\xgl\\us\\" + name + ".mp3");

                byte[] buffer = new byte[1024];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread;
                    fs.write(buffer, 0, byteread);
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (inStream != null) {
                    try {
                        inStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fs != null) {
                    try {
                        fs.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            next();
            System.out.println("ssss " + mIndex);
        }


    }


    public static class JsonRootBean implements Serializable {

        private int code;
        private String message;
        private List<Data> data;

        public void setCode(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getMessage() {
            return message;
        }

        public void setData(List<Data> data) {
            this.data = data;
        }

        public List<Data> getData() {
            return data;
        }

    }

    public static class Data implements Serializable {

        private int id;
        private int book_id;
        private int no;
        private String en_title;
        private String cn_title;
        private String en_audio;
        private String am_audio;
        private double en_title_start;
        private double en_title_end;
        private double am_title_start;
        private double am_title_end;
        private String en_question;
        private String cn_question;
        private double en_question_start;
        private double am_question_start;
        private double en_question_end;
        private double am_question_end;
        private double en_last_start;
        private double am_last_start;
        private int state;

        public void setId(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }

        public void setBook_id(int book_id) {
            this.book_id = book_id;
        }

        public int getBook_id() {
            return book_id;
        }

        public void setNo(int no) {
            this.no = no;
        }

        public int getNo() {
            return no;
        }

        public void setEn_title(String en_title) {
            this.en_title = en_title;
        }

        public String getEn_title() {
            return en_title;
        }

        public void setCn_title(String cn_title) {
            this.cn_title = cn_title;
        }

        public String getCn_title() {
            return cn_title;
        }

        public void setEn_audio(String en_audio) {
            this.en_audio = en_audio;
        }

        public String getEn_audio() {
            return en_audio;
        }

        public void setAm_audio(String am_audio) {
            this.am_audio = am_audio;
        }

        public String getAm_audio() {
            return am_audio;
        }

        public void setEn_title_start(double en_title_start) {
            this.en_title_start = en_title_start;
        }

        public double getEn_title_start() {
            return en_title_start;
        }

        public void setEn_title_end(double en_title_end) {
            this.en_title_end = en_title_end;
        }

        public double getEn_title_end() {
            return en_title_end;
        }

        public void setAm_title_start(double am_title_start) {
            this.am_title_start = am_title_start;
        }

        public double getAm_title_start() {
            return am_title_start;
        }

        public void setAm_title_end(double am_title_end) {
            this.am_title_end = am_title_end;
        }

        public double getAm_title_end() {
            return am_title_end;
        }

        public void setEn_question(String en_question) {
            this.en_question = en_question;
        }

        public String getEn_question() {
            return en_question;
        }

        public void setCn_question(String cn_question) {
            this.cn_question = cn_question;
        }

        public String getCn_question() {
            return cn_question;
        }

        public void setEn_question_start(double en_question_start) {
            this.en_question_start = en_question_start;
        }

        public double getEn_question_start() {
            return en_question_start;
        }

        public void setAm_question_start(double am_question_start) {
            this.am_question_start = am_question_start;
        }

        public double getAm_question_start() {
            return am_question_start;
        }

        public void setEn_question_end(double en_question_end) {
            this.en_question_end = en_question_end;
        }

        public double getEn_question_end() {
            return en_question_end;
        }

        public void setAm_question_end(double am_question_end) {
            this.am_question_end = am_question_end;
        }

        public double getAm_question_end() {
            return am_question_end;
        }

        public void setEn_last_start(double en_last_start) {
            this.en_last_start = en_last_start;
        }

        public double getEn_last_start() {
            return en_last_start;
        }

        public void setAm_last_start(double am_last_start) {
            this.am_last_start = am_last_start;
        }

        public double getAm_last_start() {
            return am_last_start;
        }

        public void setState(int state) {
            this.state = state;
        }

        public int getState() {
            return state;
        }

    }
}
